package dst.ass1.jpa.model.impl;

import javax.persistence.Embedded;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;

import dst.ass1.jpa.model.IAddress;
import dst.ass1.jpa.model.IPerson;

/**
 * 1.1.b.
 * inheritance strategies
 * http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_mapping_inher.html
 * 
 * The InheritanceType.JOINED strategy uses a different table for each class in the hierarchy. 
 * Each table only includes state declared in its class. Thus to load a subclass instance, the JPA implementation must read 
 * from the subclass table as well as the table of each ancestor class, up to the base entity class. 
 * 
 * -Advantages-
 *  The joined strategy has the following advantages:
 * 
 * Using joined subclass tables results in the most normalized database schema, meaning the schema with the LEAST spurious or 
 * REDUNTANT data.
 * 
 * As more subclasses are added to the data model over time, the only schema modification that needs to be made is the 
 * addition of corresponding subclass tables in the database (rather than having to change the structure of existing tables).
 * 
 * Relations to a base class using this strategy can be loaded through standard joins and can use standard foreign keys, as 
 * opposed to the machinations required to load polymorphic relations to table-per-class base types, described below. 
 * 
 * Searches in one table are faster if you search for a explicit type
 * 
 * -Disadvantages-
 * Aside from certain uses of the table-per-class strategy described below, the joined strategy is often the slowest of the 
 * inheritance models. Retrieving any subclass requires one or more database joins, and storing subclasses requires multiple 
 * INSERT or UPDATE statements. This is only the case when persistence operations are performed on subclasses; if most operations 
 * are performed on the least-derived persistent superclass, then this mapping is very fast. 
*/

@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements IPerson{
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	private String firstName;
	
	private String lastName;
	
	@Embedded  //is embedded like Membership but not an Id
	private Address address;
	
	public Person (){}
	
	@Override
	public Long getId() {
		return id;
	}
	
	@Override
	public void setId(Long id) {
		this.id = id;
	}
	
	@Override
	public String getFirstName() {
		return firstName;
	}
	
	@Override
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	@Override
	public String getLastName() {
		return lastName;
	}
	
	@Override
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	@Override
	public IAddress getAddress() {
		return address;
	}
	
	@Override
    public void setAddress(IAddress address) {
		if (address instanceof Address)
		      this.address = (Address) address;
	}

}
